//
//  RootViewController.m
//  WWallet
//
//  Created by zhangfeng on 9/18/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "AddViewController.h"
#import "IWExpenseItem.h"
#import "DetailViewController.h"
#import "EditViewController.h"
#import "IWWalletCell.h"
#import "SumViewController.h"

@implementation RootViewController

@synthesize expenses;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"WWallet";
    
    
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(statisticClicked)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClicked)];
    
    self.tableView.allowsSelectionDuringEditing = YES;
    
    IWExpenseItem *item = [IWExpenseItem new];
    [item checkSchema];
    [item release];
    
    self.expenses = [IWExpenseItem getList];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.expenses = [IWExpenseItem getList];
    [self.tableView reloadData];
    
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [expenses count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    IWWalletCell *cell = (IWWalletCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[IWWalletCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];    
    }
    // Configure the cell.
    
    cell.primaryLabel.text = [[self.expenses objectAtIndex:indexPath.row] Title];
    cell.secondaryLabel.text = [NSString stringWithFormat:@"%.2f", [[self.expenses objectAtIndex:indexPath.row] Amount]];
    cell.mainImage.image = [UIImage imageNamed:[[self.expenses objectAtIndex:indexPath.row] IsExpense]==YES?@"expense.png":@"income.png"];
    
    //cell.text = [[self.expenses objectAtIndex:indexPath.row] Title];
    //cell.font = [UIFont fontWithName:@"verdana" size:12];
    
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"you are deleting row: %d",indexPath.row);
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source.
        IWExpenseItem *deletingItem = [self.expenses objectAtIndex:indexPath.row];
        [deletingItem remove];
        self.expenses = [IWExpenseItem getList];
        
        // Remove the row from the table view
        NSMutableArray *array = [[NSMutableArray alloc] init];  
        [array addObject: indexPath];  
        [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
        [array release];
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}



// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.tableView.editing)
    {
        EditViewController *editview = [[EditViewController alloc] initWithNibName:@"EditViewController" bundle:nil];
        editview.expense = [self.expenses objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:editview animated:YES];
        
        [editview release];
    }
}

-(void) addClicked
{
    AddViewController *controller = [[AddViewController alloc] initWithNibName:@"AddViewController" bundle:nil];
    
    controller.title = @"添加";
    
    [self.navigationController pushViewController:controller animated:YES];
    
    [controller release];        
}

-(void) editClicked
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    if(self.tableView.editing){
        self.navigationItem.rightBarButtonItem.title = @"完成";
        self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStyleDone;
    }
    else
    {
        self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStylePlain;
    }
}


-(void) statisticClicked
{
    SumViewController *controller  = [[SumViewController alloc] initWithNibName:@"SumViewController" bundle:nil];
    
    controller.title = @"统计信息";
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}



-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellAccessoryDetailDisclosureButton; 
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    
    controller.expense = [self.expenses objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:controller animated:YES];
    
    [controller release];     
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

@end
